const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        dataValues: {
            estimateType:'0',
            releaseTime:'1',
            chargeRules: {
                topPrice: null,			
                freeTime: null,
                chargeRuleDetails:[						
                    {
                     endStepTime: "",   			
                     stepTime: null,   			
                     crdPrice: null   			
                    }
                ]
            }
            
        },
        estimateTypeList:[
            {
                value:'0',
                text:'按上月数据'
            },
            {
                value:'1',
                text:'按历史累计数据'
            }
        ],
        releaseTimeList:[
            {
                value:'1',
                text:'1年'
            },
            {
                value:'2',
                text:'2年'
            },
            {
                value:'3',
                text:'3年'
            },
            {
                value:'4',
                text:'4年'
            },
            {
                value:'5',
                text:'5年'
            },
        ],
        companyTypeList:[],
        sourceList: [],
        projectList: [],
        showContacts: false,
        contactObj: {},
        contactTypeList: [],
        recovery:0.00,
        surplus:0.00,
        onBeforeClose(action, done) {
            if (action == 'confirm') {
                return false
            } else {
                return true
            }
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        
    },
    //获取数据字典
    getProjectList(code) {
        let that = this
        lbbTaskService.getDictByCode({
            code: code
        }, function (res) {
            if (code == 'source_channel') {
                res.data.unshift({
                    text: '请选择',
                    value: ''
                })
                that.setData({
                    sourceList: res.data,
                })
            }else if (code == 'contact_information') {
                that.setData({
                    contactTypeList: res.data
                })
            }else if(code == 'taskProject'){
                that.setData({
                    projectList: res.data
                })
            }
        })
    },
    //根据code获取分类字典
    getAllCategoryAndSubList(code){
        let that=this
        lbbTaskService.getAllCategoryAndSubList({code},function(res){
            that.setData({
                companyTypeList:res.data
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    //投放数量
    onChangeDeviceCount(e) {
        let input = e.detail
        let regex = /^\d+$/;
        if (!regex.test(input)) {
            wx.showToast({
                title: '请输入数字',
            })
            return
        }
        this.data.dataValues.deviceCount = e.detail
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    // 医院分成比例
    onChangeHospitalScale(e) {
        let input = e.detail
        let regex = /^\d+$/;
        if (!regex.test(input)) {
            wx.showToast({
                title: '请输入数字',
            })
            return
        }
        this.data.dataValues.hospitalScale = e.detail
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    // 代理分成比例
    onChangeAgentScale(e) {
        let input = e.detail
        let regex = /^\d+$/;
        if (!regex.test(input)) {
            wx.showToast({
                title: '请输入数字',
            })
            return
        }
        this.data.dataValues.agentScale = e.detail
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    // 保洁费用
    onChangeCleaingFees(e) {
        let input = e.detail
        let regex = /^\d+$/;
        if (!regex.test(input)) {
            wx.showToast({
                title: '请输入数字',
            })
            return
        }
        this.data.dataValues.cleaningFees = e.detail
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    // 24小时封顶收费
    onChangeTopPrice(e) {
        let input = e.detail
        let regex = /^\d+$/;
        if (!regex.test(input)) {
            wx.showToast({
                title: '请输入数字',
            })
            return
        }
        this.data.dataValues.chargeRules.topPrice = e.detail
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    // 24内免费时长/分钟
    onChangeFreeTime(e) {
        let input = e.detail
        let regex = /^\d+$/;
        if (!regex.test(input)) {
            wx.showToast({
                title: '请输入数字',
            })
            return
        }
        this.data.dataValues.chargeRules.freeTime = e.detail
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    // 阶段单位时长
    onChangeStepTime(e) {
        console.log(e)
        let index = e.currentTarget.dataset.index
        let input = e.detail.value
        let regex = /^\d+$/;
        if (!regex.test(input)) {
            wx.showToast({
                title: '请输入数字',
            })
            return
        }
        this.data.dataValues.chargeRules.chargeRuleDetails[index].stepTime = input
        this.setData({
            dataValues:this.data.dataValues
        })
    },
    onChangeCrdPrice(e) {
        let index = e.currentTarget.dataset.index
        let input = e.detail.value
        let regex = /^\d+$/;
        if (!regex.test(input)) {
            wx.showToast({
                title: '请输入数字',
            })
            return
        }
        this.data.dataValues.chargeRules.chargeRuleDetails[index].crdPrice = input
        this.setData({
            dataValues:this.data.dataValues
        })
    },
    // 结束时间
    onChangeEndStepTime(e) {
        console.log(e)
        let input = e.detail.value
        let index = e.currentTarget.dataset.index
        let regex = /^\d+$/;
        if (!regex.test(input)) {
            wx.showToast({
                title: '请输入数字',
            })
            return
        }
        // if (index>1) {
        //     if(this.data.dataValues.chargeRules.chargeRuleDetails[index-1].endStepTime>=input) {
        //         wx.showToast({
        //             title: '时长必须大于上一阶段的时长',
        //         })
        //         return
        //     }
        // }
        this.data.dataValues.chargeRules.chargeRuleDetails[index].endStepTime = input
        this.setData({
            dataValues:this.data.dataValues
        })
    },
    // 删除
    delByIndex(e) {
        console.log(e)
        let index = e.currentTarget.dataset.index
        this.data.dataValues.chargeRules.chargeRuleDetails.splice(index,1)
        this.setData({
            dataValues:this.data.dataValues
        })
    },
    //选择医院
    selectHospital() {
        let that=this
        wx.navigateTo({
          url: '/pkgCost/pages/select-hospital/index',
        })
    },
    //打开公司类型选择
    onBindCompanyType(){
        this.setData({
            showCompanyType:true
        })
    },
    //代理公司类型选择回调
    onClickItem(e){
        this.data.dataValues.agentCompanyTypeText = e.detail.text
        this.data.dataValues.agentCompanyType = e.detail.id
        this.data.dataValues.agentParentCompanyType =this.data.companyTypeList[e.detail.index].id
        this.data.dataValues.mainActiveIndex = e.detail.index
        this.setData({
            showInteractPerson: false,
            dataValues: this.data.dataValues
        })
    },
    // 预估方式
    onChangeEstimateType(e) {
        this.data.dataValues.estimateType = e.detail
        console.log(e)
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    // 投放时间
    onChangeReleaseTime(e) {
        this.data.dataValues.releaseTime = e.detail
        console.log(e)
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    //来源渠道
    onChangeSource(e) {
        this.data.dataValues.sourceChannel = e.detail
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    //所属项目
    onChangeCheckbox(e) {
        this.data.dataValues.projectType = e.detail
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    //代理类型
    onChangeAgentType(e){
        this.data.dataValues.agentType = e.detail
        this.setData({
            dataValues: this.data.dataValues
        })
    },
    //打开新增联系人
    addContacts() {
        this.setData({
            showContacts: true
        })
    },
    //联系人姓名回调
    onChangeUserName(e) {
        this.data.contactObj.name = e.detail
        this.setData({
            contactObj: this.data.contactObj
        })
    },
    
    addItem() {
        let that = this
        that.data.dataValues.chargeRules.chargeRuleDetails.push(
            {
                endStepTime:'',
                stepTime:'',
                crdPrice:''
            }
        )
        that.setData({
            dataValues:that.data.dataValues
        })
    },
    reset() {
        let that = this
        let chargeRules = {
            topPrice: null,			
            freeTime: null,
            chargeRuleDetails:[						
                {
                    endStepTime: "",   			
                    stepTime: null,   			
                    crdPrice: null   			
                }
            ]
        }
        that.data.dataValues.chargeRules = chargeRules
        that.setData({
            dataValues:that.data.dataValues
        })
    },
    //数字校验
    inputNumber(e){
        let input = e.detail.value
        let regex = /^\d+$/;
        if (!regex.test(input)) {
            wx.showToast({
                title: '请输入数字',
            })
            return
        }

        // if (this.data.startEquCode && this.data.startEquCode > input) {
        //     this.setData({
        //         errEndColor: 'border:1px solid red;'
        //     })
        //     wx.showToast({
        //         title: '结束不可小于起始',
        //     })
        //     return
        // }
    },
    //提交
    btnSubmit() {
        let that = this
        let obj=JSON.parse(JSON.stringify(that.data.dataValues))
       if(obj.hospitalId&&obj.estimateType&&obj.releaseTime&&obj.deviceCount&&obj.chargeRules.topPrice&&obj.chargeRules.freeTime&&that.valiChargeRuleDetails(obj.chargeRules.chargeRuleDetails)){
           lbbTaskService.getEstimatedRevenue({...obj},function(res){
                that.setData({
                    surplus:res.data.surplus,
                    recovery:res.data.deviceRecovery
                })
           })
       }else{
           that.openPrompt()
       }
    },
    valiChargeRuleDetails(list) {
        let valiate = true
        for(let i=0;i<list.length;i++) {
            if(i==0) {
                if(!list[i].stepTime || !list[i].crdPrice) {
                    valiate = false
                }
            } else {
                if(!list[i].endStepTime || !list[i].stepTime || !list[i].crdPrice) {
                    valiate = false
                }
            }
        }
        return valiate
    },
    //校验提示
    openPrompt() {
        wx.showToast({
            title: '请检查必填项',
            icon: 'error',
            duration: 2000
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})